package com.vrsys.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import com.vrsys.domain.Order;
import com.vrsys.domain.session.Account;
import com.vrsys.domain.session.Cart;
import com.vrsys.service.OrderService;
import com.vrsys.util.Constants;
import com.vrsys.view.PageQuery;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private Cart cart;
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/ordergener.od", method = RequestMethod.GET)
    public ModelAndView generateOrder(ModelAndView mv,
            @ModelAttribute("locale") Locale locale,
            @SessionAttribute(Constants.SESSION_ACCOUNT_LABLE) Account account) {

        mv.setViewName("order");
        if(cart.getProducts().isEmpty()) {
            return mv;
        }

        Order order = orderService.saveOrder(cart.getProducts(),cart.getAmount(),account);
        cart.cleanCart();

        SimpleDateFormat fomatter = new SimpleDateFormat(Constants.SECONDS_FORMATTER, locale);
        order.setOrderCreateTime(Calendar.getInstance(locale).getTime());
        order.setOrderDisplayTime(fomatter.format(order.getOrderCreateTime()));
        mv.addObject("order", order);

        return mv;
    }

    /**
     * @param mv
     * @param locale
     * @param account
     * @return
     */
    @RequestMapping(value = "/orders.od", method = RequestMethod.GET)
    public ModelAndView getHistoryOrders(ModelAndView mv,
            @ModelAttribute("locale") Locale locale,
            @SessionAttribute(Constants.SESSION_ACCOUNT_LABLE) Account account) {
        PageQuery<Order> pageQuery = new PageQuery<Order>();
        orderService.pagingHistoryOrders(pageQuery,account);
        List<Order> orders = pageQuery.getData();
        SimpleDateFormat fomatter = new SimpleDateFormat(Constants.SECONDS_FORMATTER, locale);
        for(Order order : orders)
            order.setOrderDisplayTime(fomatter.format(order.getOrderCreateTime()));
        mv.addObject("pageQuery", pageQuery);
        mv.setViewName("orders");
        return mv;
    }

    /*
     * This is Ajax calling.
     * @RequestBody cannot be used here since the MediaType is
     */
    @RequestMapping(value = "/moreorders.od",method = RequestMethod.POST, consumes=MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody PageQuery<Order> queryMoreOrders(PageQuery<Order> pageQuery,
            @ModelAttribute("lang") String lang,
            @ModelAttribute("locale") Locale locale,
            @SessionAttribute(Constants.SESSION_ACCOUNT_LABLE) Account account) {
        pageQuery.setLanguage(lang);
        orderService.pagingHistoryOrders(pageQuery,account);
        SimpleDateFormat fomatter = new SimpleDateFormat(Constants.SECONDS_FORMATTER, locale);
        for(Order order : pageQuery.getData())
            order.setOrderDisplayTime(fomatter.format(order.getOrderCreateTime()));
        return pageQuery;
    }

    /*@RequestMapping(value = "/cartlist.od", method = RequestMethod.GET)
    public ModelAndView queryProducts(ModelAndView mv) {
        mv.addObject("products", cart.getProduct());
        mv.addObject("amount", cart.getAmount());
        mv.setViewName("cartlist");
        return mv;
    }*/

    /*@RequestMapping(value = "/{pid}/removeproduct.od", method = RequestMethod.POST)
    public @ResponseBody void removeFromCart(@PathVariable("pid") int pId) {
        cart.removeProduct(pId);
    }*/
}
